using System;
using System.Reflection;
using CommonDomain;
using CommonDomain.Persistence;
using CommonDomain.Persistence.EventStore;

namespace Sponsorship.Infrastructure
{
    /// <summary>
    /// Constructs instances of domain aggregates without invoking the usual domain logic. This is used by <see cref="EventStoreRepository"/> when
    /// an aggregate needs to be hydrated via replaying a series of events.
    /// </summary>
    public class AggregateFactory : IConstructAggregates
    {
        public IAggregate Build(Type type, Guid id, IMemento snapshot)
        {
            return Activator.CreateInstance(type, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance, null, new object[] { id, snapshot }, null) as IAggregate;
        }
    }
}